import JsonAsset = cc.JsonAsset;
import Toast from "../Script/Toast";

const {ccclass, property} = cc._decorator;

export enum dataName {
    time = "time",
    power = "power",
}

@ccclass
export default class ControlScript {

    //本地存
    static saveData(key: string, value) {
        cc.sys.localStorage.setItem(key, JSON.stringify(value));
    }

    //本地取
    static loadData(key: string) {
        return cc.sys.localStorage.getItem(key);
    }

    //监听是否切换前台
    static gameChangeShowState(callback) {
        cc.game.on(cc.game.EVENT_SHOW, callback());
    }

    //监听是否切换后台
    static gameChangeHideState(callback) {
        cc.game.on(cc.game.EVENT_HIDE, callback());
    }

    //获取数据
    static instantiate(key, callback?) {
        cc.loader.loadRes("Prefab/" + key, (err, res) => {
            let view = cc.instantiate(res);
            if (callback) {
                callback(view);
            }
            view.parent = cc.find("Canvas");
        })
    }

    //获取json
    static loadJson(key, callback) {
        cc.loader.loadRes(key, JsonAsset, (err, res) => {
            callback(res);
        })
    }

    //获取一个预制件
    static loadRes(key, callback) {
        cc.loader.loadRes(key, (err, res) => {
            callback(res);
        })
    }

    //克隆数组
    static cloneArray(childrenArray, mainArray) {
        for (let i = 0; i < mainArray.length; i++) {
            childrenArray[i] = mainArray[i];
        }
    }

    //输出一个提示语
    static createToast(string) {
        console.log("=======================生成提示语句");
        ControlScript.instantiate("toast", (view)=>{
            view.getComponent(Toast).init(string);
        })
    }
}
